package com.zou.lotterylucky.data

import android.os.SystemClock
import com.zou.lotterylucky.Constants
import com.zou.lotterylucky.LotteryApp
import com.zou.lotterylucky.utils.SPUtils
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*

class TimeLimit(obj: JSONObject) {
    var start = obj["start"] as String
    var end = obj["end"] as String
    var disable = obj["disable"] as Int
    var enable: Boolean? = false

    fun isEnable(): Boolean {
        return enable!!


//        val format = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale("nl", "NL")).apply {
//            timeZone = TimeZone.getTimeZone("GMT-4")
//        }
//        SystemClock.elapsedRealtime()
//        val startDate = format.parse(start).time
//        val endDate = format.parse(end).time
//
//        if (disable == 1) {
//            return false
//        }
//
//        val elapsedRealTime =
//            SPUtils.get(LotteryApp.context, Constants.SP_SYNC_elapsedRealTime, 0L) as Long
//        //同步时服务器的时间
//        val serviceTimeMillis =
//            SPUtils.get(LotteryApp.context, Constants.SP_SYNC_currentTimeMillis, 0L) as Long
//
//        if (elapsedRealTime == 0L || serviceTimeMillis == 0L) {
//            //如果当前没有找到同步数据，则直接返回false
//            return false
//        }
//
//        //计算出当前时间戳
//        val nowTimeMillis = SystemClock.elapsedRealtime() - elapsedRealTime + serviceTimeMillis
//
//        val calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-4"))
//        calendar.timeInMillis = nowTimeMillis
//
//        if (calendar.time.time in (startDate + 1) until endDate) {
//            return true
//        }
//        return false
    }

    fun deleteEnable(createTime: String): Boolean {
        val format = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale("nl", "NL")).apply {
            timeZone = TimeZone.getTimeZone("GMT+0")
        }
        val startDate = format.parse(start).time
        val endDate = format.parse(end).time
        val createDate = format.parse(createTime).time
        if (disable == 1 || !isEnable()) {
            return false
        }
        if (createDate in (startDate + 1) until endDate) {
            return true
        }
        return false
    }
}